﻿package com.ufenlei.LTS {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class MessageWindow extends Sprite {
		private var father : Sprite;
		private var winBody : RoundRectBody;
		private var winHead : RoundRectHead;
		private var isOpened : Boolean;
		private var _content : Array = new Array  ;
		private var messageText : TextField;
		private var textFormat : TextFormat = new TextFormat();

		public function MessageWindow(fa : Sprite,
		                              title : String = null,
		                              message:String = null,
		                              winVisible : Boolean = false,
		                              isShadow : Boolean = true) {
			textFormat.color = 0X333333;
			textFormat.bold = true;
			textFormat.size = 15;
			messageText = new TextField();
			messageText.y = 5;
			messageText.x = 150 - messageText.width / 2;
			messageText.autoSize = "center";
			messageText.text = message;
			messageText.selectable = false;
			messageText.setTextFormat(textFormat);
			var yesButton : TextButton = new TextButton("YES");
			var cancelButton : TextButton = new TextButton("CANCEL");
			father = fa;
			var winWidth : int = 300;
			var winHeight : int = 150;
			winBody = new RoundRectBody(winWidth, winHeight - 20, 5, 5, isShadow);
			winHead = new RoundRectHead(winWidth, 25, 5, 5, title);
			winBody.y += winHead.height - 2;
			this.addEventListener(MouseEvent.MOUSE_DOWN, goTop);
			this.addChild(winHead);
			this.addChild(winBody);
			this.addChild(yesButton);
			this.addChild(cancelButton);
			if(winVisible) {
				isOpened = true;
				father.addChild(this);
			}
			yesButton.x = 20;
			yesButton.y = 2 * winHeight / 3;
			cancelButton.x = winWidth - cancelButton.width - 20;
			cancelButton.y = 2 * winHeight / 3;
			yesButton.addEventListener(MouseEvent.CLICK, chooseYes);
			cancelButton.addEventListener(MouseEvent.CLICK, chooseCancel);
			winBody.addChild(messageText);
		}

		public function setTitle(title : String) : void {
			winHead.setTitle(title);
		}

		public function addContent(child : Sprite) : void {
			winBody.addContent(child);
		}

		public function clearContent(child : Sprite) : void {
			winBody.removeContent(child);
		}

		public function clearAllContent() : void {
			winBody.removeAllContent();
		}

		public function appendContent() : void {
		}

		public function open() : void {
			var blackshadow : BlackShadow;
			if (isOpened == false) {
				blackshadow = new BlackShadow(father, winHead.headWidth, winHead.headHeight + winBody.bodyHeight, "open");
				blackshadow.x = this.x + winHead.headWidth / 2;
				blackshadow.y = this.y;
				blackshadow.addEventListener("Shadow_OPEN", openWindow);
				isOpened = true;
			}
		}

		public function close() : void {
			var blackshadow : BlackShadow;
			if (isOpened == true) {
				father.removeChild(this);
				blackshadow = new BlackShadow(father, winHead.headWidth, winHead.headHeight + winBody.bodyHeight, "close");
				blackshadow.x = this.x + winHead.headWidth / 2;
				blackshadow.y = this.y + winHead.headHeight + winBody.bodyHeight / 2;
				isOpened = false;
			}
		}

		
		private function chooseYes(evt : Event) : void {
			close();
			this.dispatchEvent(new Event(Event.CLOSE));
		}

		private function chooseCancel(evt : Event) : void {
			close();
			this.dispatchEvent(new Event(Event.CANCEL));
		}

		private function openWindow(evt : Event) : void {
			father.addChild(this);
			this.dispatchEvent(new Event(Event.OPEN));
		}

		private function goTop(evt : MouseEvent) : void {
			father.setChildIndex(this, father.numChildren - 1);
		}

		private function fliter() : void {
			var shadow : DropShadowFilter = new DropShadowFilter(4, 60, 0, 1.0, 20, 20, 1.0, 3);
			this.filters = [shadow];
			var filtersArray : Array = new Array(shadow);
			this.filters = filtersArray;
		}
	}
}